<?php

/**
 * This is the model class for table "edu_article".
 *
 * The followings are the available columns in table 'edu_article':
 * @property integer $id
 * @property string $title
 * @property string $content
 * @property string $tag
 * @property integer $categoryid
 * @property integer $createtime
 * @property integer $updatetime
 * @property integer $parentid
 * @property integer $userid
 * @property integer $views
 * @property integer $comments
 */
class Link extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Article the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'edu_link';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name,url', 'required'),
			
			array('name', 'length', 'max'=>100),
			array('url,exchangeurl', 'length', 'max'=>200),
			array('createtime,sort', 'numerical', 'integerOnly'=>true),
			array('name', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(			
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => '名称',
			'url'=>'网址',
			'createtime' => '发布时间',
			'exchangeurl'=>'交换地址',
			'sort'=>'排序'
			
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->compare('name',$this->name,true);
		$criteria->order='sort,createtime desc';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>Yii::app()->params["pagesize"]
				)
		));
	}

	public function beforeSave()
	{
		if($this->isNewRecord)
		{
			$this->createtime = time();
		}
		if(!stristr(strtolower($this->url),"http://"))  $this->url='http://'.$this->url;
		if(!stristr(strtolower($this->exchangeurl),"http://"))  $this->exchangeurl='http://'.$this->exchangeurl;
		Yii::app()->cache->delete('web-links');
		return parent::beforeSave();
	}

	public function beforeDelete()
	{
		Yii::app()->cache->delete('web-links');
		return parent::beforeDelete();
	}
}